import { _decorator, Component, Node, Prefab, instantiate, ModelComponent, Texture2D, LabelComponent, RigidBodyComponent } from "cc";
import { ballTypeMap } from '../ball/ballTypeMap'
import { ballUnit } from "../ball/ballUnit";
const { ccclass, property } = _decorator;

@ccclass("BallSystem")
export class BallSystem extends Component {

    @property(Node)
    ballPrototype: Node | null = null
    @property(Node)
    numberPrototype: Node | null = null
    @property(Node)
    ballLayout: Node | null = null
    @property(Node)
    numberLayout: Node | null = null

    @property
    BallFriction: number = 0.2

    @property
    GroundFriction: number = 0.2

    @property([Texture2D])
    ballTexture: Texture2D[] = []

    start() {
        window['BallSystem'] = this
    }


    public addBall(ballNumber: number = 0): Node {
        console.log('addBall', ballNumber)
        let obj = instantiate(this.ballPrototype)
        let num = instantiate(this.numberPrototype)
        obj.setPosition(cc.v3(0, 5, 0))

        obj.parent = this.ballLayout
        num.parent = this.numberLayout
        num.getComponent('numberLabel').target = obj
        this.setNumber(obj, num, ballNumber)
        return obj
    }

    setNumber(ball: Node, num: Node, ballNumber: number = 0) {
        console.log('setNumber')

        //改变贴图   TODO: 将全部贴图补全就可用 (先不用贴图显示) 
        // let material = ball.getComponent(ModelComponent).materials[0]
        // material.setProperty('baseColorMap', this.ballTexture[ballTypeMap.get(ballNumber)])

        //改变数字
        let label = num.getComponent(LabelComponent)
        label.string = ballNumber + ''

        //改属性
        ball.getComponent(ballUnit).Num = ballNumber
    }


}
